package com.autonavi.yunda.yunji.common.exception;

import org.apache.commons.lang.StringUtils;

/**
 * @author wangchunsheng
 * @date 2021/08/16
 */
public class AmapPreconditions {
    private AmapPreconditions() {
    }

    public static void checkArgument(boolean expression) {
        checkArgument(expression, AmapExceptionCode.COMMON_ILLEGAL_PARAM);
    }

    /**
     * 异常返回给用户,不打errorLog
     */
    public static void checkArgumentForUser(boolean expression, String message) {
        checkArgument(expression, AmapExceptionCode.COMMON_SERVER_WARN, message);
    }

    public static void checkArgument(boolean expression, AmapExceptionCode exceptionCode) {
        if (!expression) {
            throw new AmapException(exceptionCode);
        }
    }

    public static void checkArgument(boolean expression, AmapExceptionCode exceptionCode, String message) {
        if (!expression) {
            throw new AmapException(exceptionCode, message);
        }
    }

    public static <T> T checkNotNull(T reference, String message) {
        if (reference == null) {
            throw new AmapException(AmapExceptionCode.COMMON_NULL_POINT, message);
        }
        return reference;
    }


    public static <T> T checkNotNull(T reference) {
        if (reference == null) {
            throw new AmapException(AmapExceptionCode.COMMON_NULL_POINT);
        }
        return reference;
    }

    public static <T> T[] checkNotNull(T... reference) {
        for (T r : reference) {
            if (r == null) {
                throw new AmapException(AmapExceptionCode.COMMON_NULL_POINT);
            }
        }
        return reference;
    }

    public static String checkNotBlank(String reference) {
        if (StringUtils.isBlank(reference)) {
            throw new AmapException(AmapExceptionCode.COMMON_ILLEGAL_PARAM);
        }
        return reference;
    }

}
